import { ListAdvertisementParm } from "@/api/advertisement/AdvertisementModel";
import { nextTick, onMounted, reactive,ref } from "vue";
import { getListApi } from "@/api/advertisement";

export default function useAdvTable(){
    //表格高度
    const tableHeight = ref(0)
    //表格数据
    const tableList = ref([])
    //查询表格参数
    const listParm = reactive<ListAdvertisementParm>({
        page: 1,
        pageSize: 10,
        totalSize: 0,
        title:'',
        goodsName:'',
        status:''
    })
    //列表
    const getList = async () => {
        let result = await getListApi(listParm)
        if (result && result.code == 200) {
            //设置表格数据
            tableList.value = result.data.data
            //设置分页总条数
            listParm.totalSize = result.data.totalPage
        }
    }
    //搜索
    const searchBtn = () => {
        getList()
    }
    //重置
    const resetBtn = () => {
        listParm.page = 1
        listParm.title = ''
        listParm.goodsName = ''
        listParm.status = ''
        getList()
    }
    //总页数改变时触发
    const sizeChange = (size: number) => {
        listParm.pageSize = size;
        getList()
    }
    //当前页数改变时触发
    const currentChange = (page: number) => {
        listParm.page = page;
        getList()
    }
    onMounted(() => {
        getList()
        //计算表格高度
        nextTick(() => {
            tableHeight.value = window.innerHeight - 435
        })
    })
    return {
        listParm,
        getList,
        searchBtn,
        resetBtn,
        tableList,
        sizeChange,
        currentChange,
        tableHeight,
    }
}